#include "StdAfx.h"
#include "WindowBuilder.h"
#include "WindowMessagesProcessor.h"
#include "Resource.h"


#define MAX_LOADSTRING 255

CWindowBuilder::CWindowBuilder(void)
{
}

CWindowBuilder::~CWindowBuilder(void)
{
}

HWND CWindowBuilder::createFullScreenWindow(HINSTANCE hInstance, 
                                            unsigned int width, 
                                            unsigned int height, 
                                            IWindowMessagesProcessor* ptrMsgProc) const
{
   TCHAR szTitle[MAX_LOADSTRING];
   TCHAR szWindowClass[MAX_LOADSTRING];
   LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
   LoadString(hInstance, IDC_LAB2_1_MYPRACTICE, szWindowClass, MAX_LOADSTRING);
   
   WNDCLASSEX wcex;
   wcex.cbSize = sizeof(WNDCLASSEX);

   wcex.style			= CS_BYTEALIGNCLIENT;
   wcex.lpfnWndProc	= WndProc;
   wcex.cbClsExtra		= 0;
   wcex.cbWndExtra		= 0;
   wcex.hInstance		= hInstance;
   wcex.hIcon			= NULL;
   wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
   wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
   wcex.lpszMenuName	= NULL;
   wcex.lpszClassName	= szWindowClass;
   wcex.hIconSm		= NULL;
   RegisterClassEx(&wcex);

   HWND hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, 0,
      0, 0, width, height, NULL, NULL, hInstance, ptrMsgProc);

   return hWnd;
}

HWND CWindowBuilder::createWindowedModeWindow(HINSTANCE hInstance, 
                                              unsigned int width, 
                                              unsigned int height, 
                                              IWindowMessagesProcessor* ptrMsgProc) const
{
   TCHAR szTitle[MAX_LOADSTRING];
   TCHAR szWindowClass[MAX_LOADSTRING];
   LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
   LoadString(hInstance, IDC_LAB2_1_MYPRACTICE, szWindowClass, MAX_LOADSTRING);
   
   WNDCLASSEX wcex;
   wcex.cbSize = sizeof(WNDCLASSEX);

   wcex.style			= CS_BYTEALIGNCLIENT | CS_HREDRAW | CS_VREDRAW;
   wcex.lpfnWndProc	= WndProc;
   wcex.cbClsExtra		= 0;
   wcex.cbWndExtra		= 0;
   wcex.hInstance		= hInstance;
   wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB2_1_MYPRACTICE));
   wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
   wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
   wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_LAB2_1_MYPRACTICE);
   wcex.lpszClassName	= szWindowClass;
   wcex.hIconSm		= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));
   RegisterClassEx(&wcex);

   HWND hWnd = CreateWindowEx(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      0, 0, width, height, NULL, NULL, hInstance, ptrMsgProc);

   return hWnd;
}

LRESULT CALLBACK CWindowBuilder::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   // If this is a create message, trap the 'this' pointer passed in and store it within the window.
   if (message == WM_CREATE) 
   {
      SetWindowLong(hWnd, GWL_USERDATA, (LONG)((CREATESTRUCT FAR *)lParam)->lpCreateParams);
   }

   IWindowMessagesProcessor *destination = (IWindowMessagesProcessor*)GetWindowLong(hWnd, GWL_USERDATA);
   if (destination) 
   {
      return destination->wndProc(hWnd, message, wParam, lParam);
   }
   else
   {
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
}
